//
//  XDScheduleMainViewController.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-5-31.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "XDScheduleMainViewController.h"
#import "DateUtil.h"
#import "NSDate+convenience.h"
#import "GetScheduleListTask.h"
#import "ScheduleInfo.h"
#import "XDScheduleAddViewController.h"

@interface XDScheduleMainViewController ()
{
    NSString *getScheduleListTaskGuid;
    VRGCalendarView *calendar;
    UITableView *tv;
    NSMutableArray *dataArr;
    NSMutableArray *singleDateArr;
}
@end

@implementation XDScheduleMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIButton* rightButton= [[UIButton alloc] initWithFrame:CGRectMake(0,0,40,20)];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"img_top_add"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(addSchedule) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = leftBarButtonItem;
    [leftBarButtonItem release];
    [rightButton release];

    singleDateArr = [[NSMutableArray alloc]init];
    calendar = [[VRGCalendarView alloc] init];
    calendar.delegate=self;
    [self.view addSubview:calendar];
    
    NSLog(@"%lf",[UITool getScreenHeight]);
    
    tv = [[UITableView alloc]initWithFrame:CGRectMake(0, 291, 320, [UITool getScreenHeight]-62-291)];
    [tv setDelegate:self];
    [tv setDataSource:self];
    [self.view addSubview:tv];
}

-(void)addSchedule
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:@"0" forKey:@"editFlag"];
    XDScheduleAddViewController *vc = [[XDScheduleAddViewController alloc]initWithParams:params];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)getServerScheduleListDate:(NSDate *)_date
{
    NSString *firtday = [NSString stringWithFormat:@"%@-%@",[DateUtil getCurrentYMStr:_date],@"01"];
    NSString *endDay = [NSString stringWithFormat:@"%@-%i",[DateUtil getCurrentYMStr:_date],[DateUtil numDaysInMonth:_date]];
    NSMutableDictionary *params1 = [self getPassDictionary];
    [params1 setObject:firtday forKey:@"FromDateTime"];
    [params1 setObject:endDay forKey:@"ToDateTime"];
    [params1 setObject:@"" forKey:@"KeyWord"];
    getScheduleListTaskGuid = [[[GetScheduleListTask alloc]initWithContext:self andParams:params1]startTask];
    
    
}

-(void)refreshUIWithThreadGuid:(NSString *)threadGuid andData:(NSObject *)obj
{
    if ([threadGuid isEqualToString:getScheduleListTaskGuid]) {
        NSString *bs = (NSString *)obj;
        dataArr = [[GetScheduleListTask getList:bs]retain];
        
        NSMutableArray *flagArr = [[NSMutableArray alloc]init];
        
        for (int i=0; i<dataArr.count; i++) {
            ScheduleInfo *m = [dataArr objectAtIndex:i];
            
            NSString* string = m.FromDateTime;
            NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
            [inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
            [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
            NSDate* inputDate = [inputFormatter dateFromString:string];
            
//          NSLog(@"ri:%i",[inputDate day]);
            [flagArr addObject:[NSNumber numberWithInt:[inputDate day]]];
            
        }
        
        [calendar markDates:flagArr];
        
        [self displaySingleDay:[NSDate date]];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(NSDate *)month targetHeight:(float)targetHeight animated:(BOOL)animated {
    [self getServerScheduleListDate:month];
    
}

-(void)calendarView:(VRGCalendarView *)calendarView dateSelected:(NSDate *)date {
    [self displaySingleDay:date];
}

-(void)displaySingleDay:(NSDate *)date
{
    [singleDateArr removeAllObjects];
    
    NSString *selectedDateStr = [DateUtil getCurrentYMDStr:date];
    
    NSLog(@"selectedDateStr:%@",selectedDateStr);
    for (int i=0; i<dataArr.count; i++) {
        ScheduleInfo *m = [dataArr objectAtIndex:i];
        if ([m.FromDateTime hasPrefix:selectedDateStr]) {
            NSLog(@"FromDateTime:%@",m.FromDateTime);
            [singleDateArr addObject:m];
        }
    }
    NSLog(@"singleDateArr Num:%i",singleDateArr.count);
    [tv reloadData];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"simplecell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]init];
    }
    
    ScheduleInfo *m = [singleDateArr objectAtIndex:indexPath.row];
    NSString *fromtime = [[m.FromDateTime componentsSeparatedByString:@" "]objectAtIndex:1];
    NSString *totime = [[m.ToDateTime componentsSeparatedByString:@" "]objectAtIndex:1];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@ %@",fromtime,totime,m.Subject];
    [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return singleDateArr.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    ScheduleInfo *m = [singleDateArr objectAtIndex:indexPath.row];
    [params setObject:@"1" forKey:@"editFlag"];
    [params setObject:m forKey:@"model"];
    XDScheduleAddViewController *vc = [[XDScheduleAddViewController alloc]initWithParams:params];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
